<template>
	<div>
		<el-row :gutter="10">
			<el-col :span="24">
				<el-card shadow="never" class="card">
					<div id="charts_one" style="width:100%;min-height:80vh">
						<Search size='small' :searchVisible="searchVisible" :searchData.sync="searchData"  :searchForm="searchForm" @refresh_list="index"></Search>
						<div class="btn-group" style="margin-bottom:11px;">
                           <div>
							   <el-button type="success"  size="mini" icon="el-icon-plus"  @click="add(ids)" v-if="checkPermission('/app/Stock/add.html')" >{{$t('action.add')}}</el-button>   
                               <el-button type="primary" :disabled="single" size="mini" icon="el-icon-edit"  @click="update(ids)" v-if="checkPermission('/app/Stock/update.html')" >{{$t('action.update')}}</el-button>  
                               <el-button type="info" :disabled="single" size="mini" icon="el-icon-view"  @click="detail(ids)" v-if="checkPermission('/app/Stock/detail.html')" >{{$t('action.detail')}}</el-button>  
                               <el-button type="primary"  size="mini" icon="el-icon-edit"  @click="refreshTheStocks(ids)" v-if="checkPermission('/app/Stock/refreshTheStocks.html')" >{{$t('action.refreshTheStocks')}}</el-button>
                               <el-button type="primary"  size="mini" icon="el-icon-edit"  @click="checkNewStocks(ids)" v-if="checkPermission('/app/Stock/checkNewStocks.html')" >{{$t('action.checkNewStocks')}}</el-button>
                               <el-button type="primary"  size="mini" icon="el-icon-edit"  @click="updateStopStocks(ids)" v-if="checkPermission('/app/Stock/updateStopStocks.html')" >{{$t('action.updateStopStocks')}}</el-button>
                           </div>
                           <table-tool :searchVisible.sync="searchVisible"  :fieldList="fieldList" @refresh_list="index" :searchTool="true"></table-tool>
						</div>
					    <el-table :row-class-name="rowClass" @selection-change="selection"  @row-click="handleRowClick"  row-key="id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" v-loading="loading"  ref="multipleTable" border class="eltable" :data="list"  style="width: 100%">		   
						    <el-table-column align="center" type="selection" width="42"></el-table-column>
							<el-table-column align="center" type='' v-if="fieldList[0].checked" property="id" label="ID" show-overflow-tooltip width="70">
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[1].checked" property="short_code" :label="$t('field.Stock_short_code')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[2].checked" property="code" :label="$t('field.Stock_code')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[3].checked" property="name" :label="$t('field.Stock_name')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[4].checked" property="pinyin" :label="$t('field.Stock_pinyin')" show-overflow-tooltip width="" >
							</el-table-column>
							 <el-table-column align="center"  v-if="fieldList[5].checked" property="stop" :label="$t('field.Stock_stop')" show-overflow-tooltip width="">
								<template slot-scope="scope">
									<el-switch @change="updateExt(scope.row,'stop')" :active-value="0" :inactive-value="1" v-model="scope.row.stop"></el-switch>
								</template>
							 </el-table-column>
							 <el-table-column align="center"  v-if="fieldList[6].checked" property="close" :label="$t('field.Stock_close')" show-overflow-tooltip width="">
								<template slot-scope="scope">
									<el-switch @change="updateExt(scope.row,'close')" :active-value="0" :inactive-value="1" v-model="scope.row.close"></el-switch>
								</template>
							 </el-table-column>
							<el-table-column align="center"  v-if="fieldList[7].checked" property="status" :label="$t('field.Stock_status')" show-overflow-tooltip width="">
							       <template slot-scope="scope"> 
							          <el-tag type="primary" v-if="scope.row.status == '1'" size="mini" effect="dark">正常</el-tag>
							          <el-tag type="danger" v-if="scope.row.status == '0'" size="mini" effect="dark">删除</el-tag>
							       </template>    
							 </el-table-column>
							 <el-table-column align="center"  v-if="fieldList[8].checked" property="trade" :label="$t('field.Stock_trade')" show-overflow-tooltip width="">
								<template slot-scope="scope">
									<el-switch @change="updateExt(scope.row,'trade')" :active-value="1" :inactive-value="0" v-model="scope.row.trade"></el-switch>
								</template>
							 </el-table-column>
							 <el-table-column align="center"  v-if="fieldList[9].checked" property="stop_time" :label="$t('field.Stock_stop_time')" show-overflow-tooltip width="" >
								<template slot-scope="scope">
								           {{parseTime(scope.row.stop_time)}}
								  
									
								</template>
							 </el-table-column>
							<el-table-column align="center"  v-if="fieldList[10].checked" property="lastamount" :label="$t('field.Stock_lastamount')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[11].checked" property="lastvolumn" :label="$t('field.Stock_lastvolumn')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center>"  v-if="fieldList[12].checked" property="control_ratio" :label="$t('field.Stock_control_ratio')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center>"  v-if="fieldList[13].checked" property="control_amount" :label="$t('field.Stock_control_amount')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center>"  v-if="fieldList[14].checked" property="control_remaining" :label="$t('field.Stock_control_remaining')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center>"  v-if="fieldList[15].checked" property="pclose" :label="$t('field.Stock_pclose')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center>"  v-if="fieldList[16].checked" property="stock_value" :label="$t('field.Stock_stock_value')" show-overflow-tooltip width="" >
							</el-table-column>
							 <el-table-column align="center"  v-if="fieldList[17].checked" property="enable_time" :label="$t('field.Stock_enable_time')" show-overflow-tooltip width="" >
								<template slot-scope="scope">
								           {{parseTime(scope.row.enable_time)}}
								  
									
								</template>
							 </el-table-column>
							<el-table-column align="center>"  v-if="fieldList[18].checked" property="stock_value_market" :label="$t('field.Stock_stock_value_market')" show-overflow-tooltip width="" >
							</el-table-column>
							  <el-table-column :fixed="$store.getters.device !== 'mobile'?'right':false" :label="$t('system.operation')" align="center" width="160">
								<template slot-scope="scope">
									<div v-if="scope.row.id">
										<el-button size="mini" type="primary" @click="update(scope.row)"  v-if="checkPermission('/app/Stock/update.html')"><i class="el-icon-edit" />{{$t('action.update')}}</el-button>
										<el-button size="mini" type="danger" @click="del(scope.row)"  v-if="checkPermission('/app/Stock/delete.html')"><i class="el-icon-delete" />{{$t('action.delete')}}</el-button>
									</div>
								</template>
							  </el-table-column>
						</el-table>
						<Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
					</div>
				</el-card>
			</el-col>
		</el-row>
	    <!--添加-->
		<Add  :show.sync="dialog.addDialogStatus" size="small" @refresh_list="index"></Add>
		<!--修改-->
		<Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refresh_list="index"></Update>
		<!--查看详情-->
		<Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="small" @refresh_list="index"></Detail>
	</div>
</template>
<script>
	import {
	    index, 
	    updateExt, 
	    getUpdateInfo,
	    del,
	    refreshTheStocks, 
	    checkNewStocks, 
	    updateStopStocks, 
	} from '@/api/app/stock'
	import Search from '@/components/common/Search.vue'
	import Page from '@/components/common/Page.vue'
	import TableTool from '@/components/common/TableTool.vue'
	import { confirm,param2Obj } from '@/utils/common'
	import Add from '@/views/app/stock/add.vue' 
	import Update from '@/views/app/stock/update.vue' 
	import Detail from '@/views/app/stock/detail.vue' 
	export default {
		name:'stock',
		components: {
			Search,
			Page,
			TableTool,
	        Add,  
	        Update,  
	        Detail,  
		},
		data(){
			return {
				dialog: {
	                    addDialogStatus : false,     
	                    updateDialogStatus : false,     
	                    detailDialogStatus : false,     
				},
				fieldList:[
					{lable:'ID',checked:true},
				    {lable:this.$t('field.Stock_short_code'),checked:true},
				    {lable:this.$t('field.Stock_code'),checked:true},
				    {lable:this.$t('field.Stock_name'),checked:true},
				    {lable:this.$t('field.Stock_pinyin'),checked:true},
				    {lable:this.$t('field.Stock_stop'),checked:true},
				    {lable:this.$t('field.Stock_close'),checked:true},
				    {lable:this.$t('field.Stock_status'),checked:true},
				    {lable:this.$t('field.Stock_trade'),checked:true},
				    {lable:this.$t('field.Stock_stop_time'),checked:true},
				    {lable:this.$t('field.Stock_lastamount'),checked:true},
				    {lable:this.$t('field.Stock_lastvolumn'),checked:true},
				    {lable:this.$t('field.Stock_control_ratio'),checked:false},
				    {lable:this.$t('field.Stock_control_amount'),checked:false},
				    {lable:this.$t('field.Stock_control_remaining'),checked:false},
				    {lable:this.$t('field.Stock_pclose'),checked:false},
				    {lable:this.$t('field.Stock_stock_value'),checked:false},
				    {lable:this.$t('field.Stock_enable_time'),checked:true},
				    {lable:this.$t('field.Stock_stock_value_market'),checked:false},
				],
				ids: [],
				selectRows:[],
				selectTotal:0,
				single:true,
				multiple:true,
				list: [],
				small_tables:{},
	            addInfo:{},
	            updateInfo:{},
	            detailInfo:{},
				loading: false,
				page_data: {
					limit: 20,
					page: 1,
					total:20,
				},
				searchVisible:true,
				searchForm:[],
				searchData:{},
			}
		},
		methods: {
			index(){
				let param = {limit:this.page_data.limit,page:this.page_data.page}
				Object.assign(param, this.searchData,param2Obj(this.$route.fullPath))
				this.loading = true
				index(param).then(res => {
					this.list = res.data.data
					this.small_tables=res.sql_field_data
					this.page_data.total = res.data.total
					this.loading = false
					if(this.page_data.page == 1){
						this.searchForm = [
						        {type:'Input',label:this.$t('field.Stock_code'),prop:'code',width:'150px'},   
						        {type:'Input',label:this.$t('field.Stock_name'),prop:'name',width:'150px'},   
						        {type:'Input',label:this.$t('field.Stock_pinyin'),prop:'pinyin',width:'150px'},   
						       {type:'Select',label:this.$t('field.Stock_stop'),prop:'stop',data:[{"val":"0","key":"停牌"},{"val":"1","key":"正常"}],width:'150px'},
						       {type:'Select',label:this.$t('field.Stock_close'),prop:'close',data:[{"val":"0","key":"退市"},{"val":"1","key":"正常"}],width:'150px'},
						       {type:'Select',label:this.$t('field.Stock_status'),prop:'status',data:[{"val":"1","key":"正常","label_color":"primary"},{"val":"0","key":"删除","label_color":"danger"}],width:'150px'},
						       {type:'Select',label:this.$t('field.Stock_trade'),prop:'trade',data:[{"val":"1","key":"是"},{"val":"0","key":"否"}],width:'150px'},
						]
					}
				})
			},
			updateExt(row,field){
				if(row.id){
					updateExt({id:row.id,[field]:row[field]}).then(res => {
						this.$message({message: this.$t('system.success'), type: 'success'})
					})
				}
			},
			add(){
				this.dialog.addDialogStatus = true
			},
			update(row){
				let id = row.id ? row.id : this.ids.join(',')
				getUpdateInfo({id:id}).then(res => {
					if(res.status == 200){
						this.dialog.updateDialogStatus = true
						this.updateInfo = res.data
					}
				})
			},
			del(row){
				confirm({content:this.$t('system.confirmOperation')}).then(() => {
					let ids = row.id ? row.id : this.ids.join(',')
					del({id:ids}).then(res => {
						this.$message({message: this.$t(res.msg), type: 'success'})
						this.index()
					})
				}).catch(() => {})
			},
			detail(row){
				this.dialog.detailDialogStatus = true
				this.detailInfo = {id:row.id ? row.id : this.ids.join(',')}
			},
			refreshTheStocks(row){
				confirm({content:this.$t('system.confirmOperation')}).then(() => {
					let ids = row.id ? row.id : this.ids.join(',')
					refreshTheStocks({id:ids}).then(res => {
						this.$message({message: this.$t(res.msg), type: 'success'})
						this.index()
					})
				}).catch(() => {})
			},
			checkNewStocks(row){
				confirm({content:this.$t('system.confirmOperation')}).then(() => {
					let ids = row.id ? row.id : this.ids.join(',')
					checkNewStocks({id:ids}).then(res => {
						this.$message({message: this.$t(res.msg), type: 'success'})
						this.index()
					})
				}).catch(() => {})
			},
			updateStopStocks(row){
				confirm({content:this.$t('system.confirmOperation')}).then(() => {
					let ids = row.id ? row.id : this.ids.join(',')
					updateStopStocks({id:ids}).then(res => {
						this.$message({message: this.$t(res.msg), type: 'success'})
						this.index()
					})
				}).catch(() => {})
			},
			selection(selection) {
			    this.selectRows=selection
			    this.selectTotal=selection.length
				this.ids = selection.map(item => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			handleRowClick(row, rowIndex,event){
				if(event.target.className !== 'el-input__inner'){
					this.$refs.multipleTable.toggleRowSelection(row)
				}
			},
			rowClass ({ row, rowIndex }) {
				for(let i=0;i<this.ids.length;i++) {
					if (row.id === this.ids[i]) {
						return 'rowLight'
					}
				}
			},
		},	
		mounted(){
			this.index()
		}
	}
</script>
<style  lang="scss">
@import '@/assets/scss/common.scss'
</style>